package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);
    //老师主菜单
    public void start() {

        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    //System.out.println("添加");
                    break;
                case "2":
                    deleteTeacher();
                    //System.out.println("删除");
                    break;
                case "3":
                    updateTeacher();
                    //System.out.println("修改");
                    break;
                case "4":
                    findAllTeacher();
                    //System.out.println("查询");
                    break;
                case "5":
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入:");
                    break;
            }
        }

    }
    //修改老师
    private void updateTeacher() {
        String id;
        Teacher[] teachers=teacherService.findAllTeacher();
        if(teachers==null){
            System.out.println("查无数据,请添加后操作");
            return;
        }
        id = inputTeacherId();
        Teacher teacher = getTeacher(id);
        teacherService.updateTeacher(teacher,id);
        System.out.println("修改成功");

    }
    //键盘录入Id
    private String inputTeacherId() {
        String id;
        while(true){
            System.out.println("请输入老师工号");
            id= sc.next();
            boolean flag=teacherService.isExists(id);
            if(flag){
                break;
            }else{
                System.out.println("您输入的工号不存在,请重新输入");
            }
        }
        return id;
    }
    //删除老师
    private void deleteTeacher() {
        String id;
        Teacher[] teacher=teacherService.findAllTeacher();
        if(teacher==null){
            System.out.println("查无数据,请添加后操作");
            return;
        }
        id=inputTeacherId();
        teacherService.deleteTeacher(id);
        System.out.println("删除成功");
    }
    //查询老师
    private void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("查无数据,请添加后操作");
        } else {
            System.out.println("工号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < teachers.length; i++) {
                if (teachers[i] != null) {
                    System.out.println(teachers[i].getId() + "\t" + teachers[i].getName() + "\t" + teachers[i].getAge() + "\t\t" + teachers[i].getBirthday());

                }
            }
        }


    }
    //添加老师
    private void addTeacher() {
        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            System.out.println("请输入工号");
            id = sc.next();
            boolean flag1 = teacherService.isExists(id);
            if (flag1) {
                System.out.println("学号已被占用,请重新输入");
            } else {
                break;
            }
        }
        Teacher teacher = getTeacher(id);
        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }


    }

    private Teacher getTeacher( String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        return new Teacher(id, name, age, birthday);
    }
}
